package erfgame.frontend.desktop.view.terrain.iso.interaction;

import java.util.ArrayList;
import java.util.List;

import erfgame.core.Filter;
import erfgame.core.Pair;
import erfgame.core.entity.Entity;

public class FilteringCompositeEntityInteractionDescriptionFactoryManager
		implements EntityInteractionDescriptionFactoryManager {

	private List<Pair<Filter<Entity>,EntityInteractionDescriptionFactoryManager>> managers;
	
	public FilteringCompositeEntityInteractionDescriptionFactoryManager(List<Pair<Filter<Entity>,EntityInteractionDescriptionFactoryManager>> managers){
		this.managers = managers;
	}
	
	@SuppressWarnings(value="unchecked")
	public EntityInteractionDescriptionFactory getFactory(Entity interacter) {

		ArrayList<EntityInteractionDescriptionFactory> factories;
		factories = new ArrayList<EntityInteractionDescriptionFactory>();
		for( int i=0; i<managers.size(); i++ ) {
			Pair<Filter<Entity>, EntityInteractionDescriptionFactoryManager> pair;
			pair = managers.get(i);
			Filter<Entity> filter = pair.getFirst();
			if( filter.accept(interacter)) {
				EntityInteractionDescriptionFactory factory;
				factory = pair.getSecond().getFactory(interacter);
				if(factory != null ) {
					factories.add( factory );
				}
			}
		}
		return new CompositeEntityInteractionDescriptionFactory(factories);
	}

}
